//
//  LKStatusOriginalView.swift
//  LKWeibo
//
//  Created by like on 16/8/13.
//  Copyright © 2016年 like. All rights reserved.
//

import UIKit
import SnapKit

class LKStatusOriginalView: UIView {
    
    var OriginalViewPictureConstraint: Constraint?
    
    
    //定义一个statusViewModel
    var statusViewModel: LKStatusViewModel?{
        didSet{
            
            contentLabel.text = statusViewModel?.statusModel?.text
            headImageView.lk_setImageWithString(statusViewModel?.statusModel?.user?.profile_image_url, placeholderImgName: "avatar_default")
            nameLabel.text = statusViewModel?.statusModel?.user?.name
            
            //  statusViewModel?.statusModel?.created_at
            //时间
            timeLabel.text = dealSinaTimeStr(statusViewModel?.statusModel?.created_at ?? "")
            
            
            
            //来源
            sourceLabel.text = statusViewModel?.sinaSourceStr
            
            //会员等级
            membershipImageView.image = statusViewModel?.mbrankImage
            //认证
            avatarImageView.image = statusViewModel?.verifiedImage
            
//            printLog(statusViewModel?.statusModel?.pic_urls?.count)

            
            
//            处理配图

            OriginalViewPictureConstraint?.uninstall()
            //如果数组长度 > 0  有配图   第一步赋值
            if  statusViewModel?.statusModel?.pic_urls?.count > 0 {
                
                pictureView.pictureInfos = statusViewModel?.statusModel?.pic_urls
                
                self.snp_makeConstraints { (make) in
                    OriginalViewPictureConstraint = make.bottom.equalTo(pictureView).offset(10).constraint
                }
                //显示视图
                pictureView.hidden = false
            }else{
                
                self.snp_makeConstraints { (make) in
                    OriginalViewPictureConstraint = make.bottom.equalTo(contentLabel).offset(10).constraint
                }
                //隐藏视图
                pictureView.hidden = true
            }
            
            
            
            
        }
    }
    
//    // 处理微博等级image/// 会员等级 1-6
//    func dealMbrankImage(mbrank: Int) -> UIImage?{
//        if mbrank > 0 && mbrank < 7 {
//            return UIImage(named: "common_icon_membership_level\(mbrank)")
//        }else{
//            return UIImage(named: "common_icon_membership_expired")
//        }
//
//    }
//    
//    
//    func dealverifiedImage(verified: Int) -> UIImage?{
//        switch verified {
//        case 1:
//            return UIImage(named: "avatar_vip")
//        case 2,3,5:
//            return UIImage(named: "avatar_grassroot")
//        case 220:
//            return UIImage(named: "avatar_enterprise_vip")
//        default:
//            return UIImage(named: "avatar_vgirl")
//        }
//
//    }
    
//    func dealSinaSourceStr(source: String) -> String{
////        printLog("哈哈哈")
//        // 判断字符串是否包含"> 特殊字符
//        if source.containsString("\">") {
//            // 获取range
//            let startRange = source.rangeOfString("\">")!
//            let endRange = source.rangeOfString("</")!
//            // 字符串截取
//            let result = source.substringWithRange(startRange.endIndex..<endRange.startIndex)
//            return "来自 \(result)"
//        }
//        // 如果为空字符串
//        return "来自 那遥远的地方"
//    }
    
    
//    func dfafd(source: String) -> String{
//        
//        if source.containsString("\">") {
//            let startRange = source.rangeOfString("\">")
//            let end = source.rangeOfString("<")
//            let result = source.substringWithRange((startRange?.endIndex)!..<(end?.startIndex)!)
//            return result
//        }
//        
//        
//        
//        return "laizufdsf"
//    }
    
    
    //处理微博时间
    func dealSinaTimeStr(createAt: String) -> String{
        
        //时间格式化
        let df = NSDateFormatter()
        //设置格式
        df.dateFormat = "EEE MMM dd HH:mm:ss z yyyy"
        //本地化
        df.locale = NSLocale(localeIdentifier: "en_US")
        
        //微博时间
        let sinaDate = df.dateFromString(createAt)!
        
//        printLog(sinaDate)
        let isThisYear = sinaIsThisYear(sinaDate)
        //是今年
        if  isThisYear {
            
            let calendar = NSCalendar.currentCalendar()
            //如果是今天
            if calendar.isDateInToday(sinaDate){
                // 当前时间和微博时间 的差
                let s = Int(NSDate().timeIntervalSinceDate(sinaDate))
                // s< 60 显示 刚刚
                if s < 60 {
                    return "刚刚"
                }else if s >= 60 && s < 3600 {
                    // xx分钟前
                    return "\(s/60)分钟前"
                }else {
                    // xx小时前
                    return "\(s/3600)小时前"
                }
                //如果是昨天
            }else if calendar.isDateInYesterday(sinaDate) {
                // 如果是昨天 2014年06月14日 20:20:20 显示 昨天 20:20
                df.dateFormat = "昨天 HH:mm"
                
                //其他天
            }else{
                df.dateFormat = "MM月dd日 HH:mm"
            }
        }else{
            //不是今年  重新指定格式
            df.dateFormat = "yyyy年MM月dd日 HH:mm"
        }
        return df.stringFromDate(sinaDate)
    }
    
    //判断 微博时间  是否是今年的
    func sinaIsThisYear(sinaDate: NSDate) -> Bool{
        //时间格式化
        let df = NSDateFormatter()
        //设置格式
        df.dateFormat = "yyyy"
        //本地化
        df.locale = NSLocale(localeIdentifier: "en_US")
        //微博时间和本地市集
        let sinaTimeStr = df.stringFromDate(sinaDate)
        let currentTime = df.stringFromDate(NSDate())
        
        return sinaTimeStr == currentTime
    }
    
    
    
    
    override init(frame: CGRect) {
        super.init(frame: frame)
        setupUI()
        
    }
    
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }

    
    func setupUI(){
        backgroundColor = UIColor.whiteColor()
        //添加控件
        addSubview(headImageView)
        addSubview(nameLabel)
        addSubview(membershipImageView)
        addSubview(timeLabel)
        addSubview(sourceLabel)
        addSubview(avatarImageView)
        addSubview(contentLabel)
        addSubview(pictureView)
        
        //临时数据
//        nameLabel.text = "大哥大"
//        timeLabel.text = "12"
//        sourceLabel.text = "佛挡杀佛"
//        contentLabel.text = "绯闻绯闻任务任务发多少分违反阿范德萨发生额染发按时发多少放大是"
        
        
        //设置约束
        //01
        headImageView.snp_makeConstraints { (make) in
            make.top.leading.equalTo(10)
            make.height.width.equalTo(35)
        }
        //02
        nameLabel.snp_makeConstraints { (make) in
            make.top.equalTo(headImageView)
            make.leading.equalTo(headImageView.snp_trailing).offset(10)
        }
        //03
        membershipImageView.snp_makeConstraints { (make) in
            make.leading.equalTo(nameLabel.snp_trailing).offset(10)
            make.centerY.equalTo(nameLabel)
        }
        //04
        timeLabel.snp_makeConstraints { (make) in
            make.bottom.equalTo(headImageView)
            make.leading.equalTo(headImageView.snp_trailing).offset(10)
            
        }
        //05
        sourceLabel.snp_makeConstraints { (make) in
            make.leading.equalTo(timeLabel.snp_trailing).offset(10)
            make.bottom.equalTo(headImageView)
        }
        //06
        avatarImageView.snp_makeConstraints { (make) in
            make.centerY.equalTo(headImageView.snp_bottom)
            make.centerX.equalTo(headImageView.snp_trailing)
        }
        //07
        contentLabel.snp_makeConstraints { (make) in
            make.top.equalTo(headImageView.snp_bottom).offset(10)
            make.leading.equalTo(10)
        }

        self.snp_makeConstraints { (make) in
           OriginalViewPictureConstraint = make.bottom.equalTo(pictureView).offset(10).constraint
        }
        
        //图片约束
        
        pictureView.snp_makeConstraints { (make) in
            make.top.equalTo(contentLabel.snp_bottom).offset(10)
            make.leading.equalTo((SCREENW - 250)/2)
            //下面这个放进内部来约束
//            make.size.equalTo(CGSize(width: 100, height: 100))
        }
    }
    
    
    
    
    //懒加载 各个控件
    //01  用户头像
    private var headImageView: UIImageView = UIImageView(imgName: "avatar_default")
    //02  名字
    private lazy var nameLabel: UILabel = UILabel(textColor: UIColor.blackColor(), textSize: 15)
    //03 用户等级common_icon_membership
    private lazy var membershipImageView: UIImageView = UIImageView(imgName: "common_icon_membership")
    //04  时间
    private lazy var timeLabel: UILabel = UILabel(textColor: appThemeColor, textSize: 10)
    //05 来源
    private lazy var sourceLabel: UILabel = UILabel(textColor: UIColor.darkGrayColor(), textSize: 10)
    //06 认证avatar_vgirl
    private lazy var avatarImageView: UIImageView = UIImageView(imgName: "avatar_vgirl")
    //07 微博正文
    private lazy var contentLabel: UILabel = UILabel(textColor: UIColor.blackColor(), textSize: 15, maxWidth: SCREENW - 20)
    
    //微博图片
    private lazy var pictureView: LKStatusPictureView = LKStatusPictureView()
 
}
